import { hasOwn } from "./tools";

/**
 * 创建页面/弹窗，焦点信息基础列表
 */
export default class Pools {
  constructor() {
    this.groups = {};
    this.items = [];
    this.pointer = null;
    this.signFlag = {};
    this.signItems = {};
    this.groupList = {};
  }

  /**
   * 赋值焦点
   * @param {Creater} creater 焦点对象
   */
  setPointer(creater = null) {
    this.pointer = creater || null;
  }

  /**
   * 检测标记默认选中状态
   * @param {String} sign 节点分组标记名称
   * @returns Boolean
   */
  checkSign(sign) {
    return hasOwn(this.signFlag, sign)
      ? this.signFlag[sign]
      : (this.signFlag[sign] = false);
  }

  /**
   * 获取对应标记列表
   * @param {String} sign 标记名称
   * @returns Array|null
   */
  getSignList(sign) {
    return hasOwn(this.signItems, sign) ? this.signItems[sign] : null;
  }

  /**
   * 获取标记列表，如果没有则创建新数组
   * @param {String} sign 标记名称
   * @returns Array
   */
  checkOrCreateSignList(sign) {
    return hasOwn(this.signItems, sign)
      ? this.signItems[sign]
      : (this.signItems[sign] = []);
  }
  getInnerGroupList(group) {
    return hasOwn(this.groupList, group) ? this.groupList[group] : [];
  }

  /**
   * 注册焦点
   * @param {Creater} creater 焦点的对象
   */
  registePool(creater) {
    let _this = this;
    let { group, name, selected, sign } = creater;
    if (name === "group") {
      if (hasOwn(this.groups, sign)) {
        throw `标记为${sign}的区域已经注册过，请勿重复注册`;
      } else {
        this.groups[sign] = creater;
      }
    }
    // 添加group指向
    Object.defineProperty(creater, "__group__", {
      get() {
        return group ? _this.groups[group] : null;
      },
      set() {
        group = "";
      },
    });
    if (group) {
      this.groupList[group] || (this.groupList[group] = []);
      creater.__list__ = this.groupList[group];
      // 焦点移入对应分组列表
      this.groupList[group].push(creater);
    } else {
      creater.__list__ = this.items;
      // 焦点移入通用焦点列表
      this.items.push(creater);
    }
    if (name === "items" && sign) {
      if (this.checkSign(sign)) {
        creater.selected = false;
      } else {
        this.signFlag[sign] = selected;
      }
      /**
       * 同步推入相关标记列表
       */
      let signList = this.checkOrCreateSignList(sign);
      signList.push(creater);
      creater.__sign__ = signList;
    }
  }

  /**
   * 移除焦点对象
   * @param {Creater} creater 焦点对象
   */
  splicePool(creater) {
    let { name, sign, assign, group } = creater;
    if (name === "items") {
      // 判断是否为当前pools的焦点
      this.pointer === creater && this.setPointer();
      // 判断是否含有标记，并从对应标记列表中移除
      if (sign) {
        if (assign) {
          this.signFlag[sign] = false;
        }
        creater.__sign__ &&
          creater.__sign__.splice(creater.__sign__.indexOf(creater), 1);
        if (!creater.__sign__.length) {
          // 所有该标记的节点都卸载之后，移除该标记
          delete this.signFlag[sign];
          delete this.signItems[sign];
        }
      }
    } else if (name === "group") {
      delete this.groups[sign];
    }
    creater.__list__.splice(creater.__list__.indexOf(creater), 1);
    if (!creater.__list__.length && group) {
      delete this.groupList[sign];
    }
  }
}
